/**
 * 
 */
package com.winit.openapi.erp.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.erp.spi.product.ProductCategoryService;
import com.winit.erp.spi.product.command.QueryProductCategoryCommand;
import com.winit.erp.spi.product.vo.ProductCategory;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * fsp 资信处理
 * 
 * @author
 * @since 1.0
 */
@Controller
@RequestMapping(value = "/erp/parnterCategory")
public class PartnerCategoryInfoController extends BaseController {

    @Resource
    private ProductCategoryService productCategoryService;

    /**
     * 查询卖家产品分类
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/query")
    public String query(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryProductCategoryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = buildQueryProductCategoryCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        List<ProductCategory> productCategoryList = productCategoryService.queryProductCategory(command);
        // if (productCategoryList != null) {
        // List<Map<String, Object>> data =
        // buildReturnProductCategoryList(productCategoryList);
        responseMsg.setData(productCategoryList);
        // }

        return SUCCESS;
    }

    /**
     * 根据最后的分类编码查询所有的父分类
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        QueryProductCategoryCommand command = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = buildQueryProductCategoryCommand(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        List<ProductCategory> productCategoryList = productCategoryService.queryProductCategoryByChild(command);
        // if (productCategoryList != null) {
        // List<Map<String, Object>> data =
        // buildReturnProductCategoryList(productCategoryList);
        responseMsg.setData(productCategoryList);
        // }

        return SUCCESS;
    }

    @SuppressWarnings("unused")
    private List<Map<String, Object>> buildReturnProductCategoryList(List<ProductCategory> productCategoryList) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        Collections.reverse(productCategoryList);
        for (ProductCategory productCategory : productCategoryList) {
            map = new HashMap<String, Object>();
            map.put("name", productCategory.getName());
            map.put("productCategoryId", productCategory.getProductCategoryId());
            mapList.add(map);
        }
        return mapList;
    }

    private QueryProductCategoryCommand buildQueryProductCategoryCommand(JSONObject json) {
        QueryProductCategoryCommand command = new QueryProductCategoryCommand();
        if (json.containsKey("productCategoryParentId")) {
            command.setProductCategoryParentId(json.getLong("productCategoryParentId"));
        }
        if (json.containsKey("productCategoryId")) {
            command.setProductCategoryId(json.getLong("productCategoryId"));
        }
        if (json.containsKey("bpartnerId")) {
            command.setBpartnerId(json.getLong("bpartnerId"));
        }
        /*
         * CommandContext ctx = CommandContext.getContext(); Bpartner bpartner =
         * (Bpartner) ctx.get(ApiConstant.API_BPARTNER);
         * command.setBpartnerId(bpartner.getcBpartnerId());
         * command.setCtx(ctx);
         */
        return command;
    }
}
